<template>
  <div>
    <el-row style="margin-bottom:20px;display:flex;justify-content:space-between">
      <el-select v-model="searchData.ep_type" placeholder="请选择设备分类" style="margin-right:10px;width:20%" clearable>
        <el-option v-for="item in eqtypeArr" :key="item.value" :label="item.hash.code_name" :value="item.hash.code_id"></el-option>
      </el-select>
      <el-select v-model="searchData.status" placeholder="请选择设备状态" style="margin-right:10px;width:20%" clearable>
        <el-option v-for="item in statusArr" :key="item.value" :label="item.hash.code_name" :value="item.hash.code_id"></el-option>
      </el-select>
      <el-input placeholder="溯源方式" style="width:20%;margin-right:10px" v-model="searchData.traceability" clearable></el-input>
      <el-input placeholder="负责人" style="width:20%;margin-right:10px" v-model="searchData.charger" clearable></el-input>
      <el-input placeholder="使用人" style="width:20%;margin-right:10px" v-model="searchData.syrname" clearable></el-input>
      <el-input placeholder="规格型号" style="width:20%;margin-right:10px" v-model="searchData.scale" clearable></el-input>
    </el-row>
    <el-row style="margin-bottom:20px;display:flex;justify-content:space-between">
      <el-input placeholder="仪器设备名称" style="width:20%;margin-right:10px" v-model="searchData.ep_ename" clearable></el-input>
      <el-input placeholder="管理编号" style="width:20%;margin-right:10px" v-model="searchData.managenum" clearable></el-input>
      <el-input placeholder="出厂编号" style="width:20%;margin-right:10px" v-model="searchData.factory_num" clearable></el-input>
      <el-input placeholder="生产厂家" style="width:20%;margin-right:10px" v-model="searchData.factory" clearable></el-input>
      <el-input placeholder="检定/校准单位" style="width:20%;margin-right:10px" v-model="searchData.verifiy_company" clearable></el-input>
      <el-input placeholder="量值" style="width:20%;margin-right:10px" v-model="searchData.measurement" clearable></el-input>
    </el-row>

    <el-row style="margin-bottom:30px;display:flex;justify-content:flex-start">
      <el-input placeholder="放置地点" style="width:20%;margin-right:10px" v-model="searchData.location" clearable></el-input>
      <el-input placeholder="检测项目" style="width:20%;margin-right:10px" v-model="searchData.verifiy_project" clearable></el-input>
      <selectorg @getselectorgid="getselectorgid" style="width:20%;margin-right:10px"></selectorg>
      <el-date-picker v-model="jddate" clearable style="margin-right:15px;width:25%" type="daterange" rangeSeparator="至" startPlaceholder="本次检定/校准开始" endPlaceholder="本次检定/校准结束" valueFormat="yyyy-MM-dd"></el-date-picker>
      <el-date-picker v-model="jddate2" clearable style="margin-right:10px;width:25%" type="daterange" rangeSeparator="至" valueFormat="yyyy-MM-dd" startPlaceholder="下次检定/校准开始" endPlaceholder="下次检定/校准结束"></el-date-picker>
      <el-date-picker v-model="buydate" clearable style="margin-right:10px;width:25%" type="daterange" rangeSeparator="至" startPlaceholder="购置开始日期" endPlaceholder="购置开始日期" valueFormat="yyyy-MM-dd"></el-date-picker>
      <el-button size="mini" type="primary" @click="geteqlist(1)">搜索</el-button>
    </el-row>

    <el-row>
      <div style="float:left">
        <el-button size="mini" type="primary" @click="showneweq">添加主设备</el-button>
        <el-button size="mini" type="primary" @click="addsoneq">添加子设备</el-button>
        <el-button size="mini" type="primary" @click="editeq">编辑</el-button>
        <el-button size="mini" type="danger" @click="deleteeq">删除</el-button>
        <el-button size="mini" type="info" @click="daochu">导出</el-button>
      </div>

      <div style="float:right">
        <el-button size="mini" type="primary" @click="createeqyear">年度检定生成</el-button>
        <el-button size="mini" type="primary" @click="createeqyear2">选中检定生成</el-button>
      </div>
    </el-row>
    <el-divider></el-divider>
    <eqtable :eqlist="eqlist" :pagobj="pagobj" :choosedp.sync="choosedp" :ismanager="true" :eqtype="eqtype"></eqtable>
    <eqtable :eqlist="neweqlist" :pagobj="pagobj" v-show="false" :choosedp.sync="choosedp" :ismanager="true" :eqtype="eqtype" id="tableExcel2ssss"></eqtable>
    <addeq v-if="isneweq" :isedit="isedit" :editdata="editdata" :eqtype="eqtype"></addeq>
    <addeq3 v-if="addeq3" :maineq="maineq" :eqtype="eqtype"></addeq3>
    <el-dialog :title="title" :visible.sync="showcheckeq" width="40%">
      <el-form labelWidth="200px">
        <el-form-item label="设备检定/期间检查年度">
          <el-date-picker v-model="checkeqtime" type="year" style="width:100%" placeholder="选择年" valueFormat="yyyy"></el-date-picker>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="showcheckeq = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="docreateeqyear" v-if="isall">生 成</el-button>
        <el-button size="mini" type="primary" @click="docreateeqyear2" v-else>生 成</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import addeq from '~/components/apparatus/eqmanager/addeq.vue';
import addeq3 from '~/components/apparatus/eqmanager/addeq3.vue';
import eqtable from '~/components/apparatus/eqmanager/eqtable.vue';
import selectorg from '~/components/common/selectorg.vue';
import FileSaver from 'file-saver';
import XLSX from 'xlsx';

export default {
  props: {
    eqtype: {
      type: String,
      required: true,
    },
  },
  components: {
    addeq,
    eqtable,
    addeq3,
    selectorg,
  },
  data () {
    return {
      ts: this.newStore(),
      title: '年度检定生成',
      addeq3: false,
      isall: false,
      swage: '',
      wdate: '',
      isneweq: false,
      isedit: false,
      showcheckeq: false,
      innerVisible22: false,
      checkeqtime: '',
      eqtypeArr: [],
      editdata: {},
      neweqlist: [],
      maineq: {},
      choosedp: [],
      statusArr: [],
      jddate: ['', ''],
      jddate2: ['', ''],
      buydate: ['', ''],
      pagobj: {
        currentPage3: 1,
        pagesize: 10,
        total: 10,
      },
      eqlist: [],
      searchData: {
        eqtype: '',
        orgid: '',
      },
      newid: '',
    };
  },
  created () {
    this.getcodeData('0403', '04', 'statusArr');
    this.getcodeData('0402', '04', 'eqtypeArr');
  },
  mounted () {
    this.searchData.ep_type = this.eqtype;
    // this.geteqlist();
  },
  methods: {
    getselectorgid (val) {
      this.searchData.orgid = val;
    },
    // 编辑人员
    addsoneq () {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选设备',
        });
        return;
      }
      if (this.choosedp.length > 1) {
        this.$message({
          type: 'info',
          message: '只能编辑一个设备',
        });
        return;
      }
      this.maineq = this.choosedp[0];
      this.reload('addeq3');
    },
    // 选中检定生成
    docreateeqyear2 () {
      const deletearr = [];
      for (let i = 0; i < this.choosedp.length; i += 1) {
        deletearr.push(this.choosedp[i].hash.id);
      }
      this.$confirm('确认生成选中的设备检定?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const req = {
            handle:
              'DD5D108B99FC12B72DC0B97F927BBE3D40E8946194EC3A0EFA9D5B6EA8B4A60CFDC3623D0DE2A04A',
            id: deletearr,
            year: this.checkeqtime,
          };
          this.spost(this.ts, '/mainservlet', req).then(() => {
            this.$message({
              type: 'success',
              message: '生成成功',
            });
            this.showcheckeq = false;
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    // 年度检定生成
    docreateeqyear () {
      this.$confirm('确认生成年度检定?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const req = {
            handle:
              'DD5D108B99FC12B72DC0B97F927BBE3D40E8946194EC3A0E93EC1B247FDDB046',
            year: this.checkeqtime,
          };
          this.spost(this.ts, '/mainservlet', req).then(() => {
            this.$message({
              type: 'success',
              message: '生成成功',
            });
            this.showcheckeq = false;
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    // 年度生成计划
    createeqyear () {
      this.isall = true;
      this.title = '年度检定生成';
      this.checkeqtime = '';
      this.showcheckeq = true;
    },
    // 年度生成计划
    createeqyear2 () {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选设备',
        });
        return;
      }
      this.isall = false;
      this.title = '选中检定生成';
      this.checkeqtime = '';
      this.showcheckeq = true;
    },
    // 编辑人员
    editeq () {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选设备',
        });
        return;
      }
      if (this.choosedp.length > 1) {
        this.$message({
          type: 'info',
          message: '只能编辑一个设备',
        });
        return;
      }
      this.isedit = true;
      const req = {
        handle:
          'DD5D108B99FC12B72DC0B97F927BBE3DD7F2412F8A4ED57CBFF0B9210751F692',
        id: this.choosedp[0].hash.id,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.editdata = ts.d.data;
        this.reload('isneweq');
      });
    },
    // 公共方法获取代码项
    getcodeData (supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
      });
    },
    // 删除设备
    deleteeq () {
      if (this.choosedp.length == 0) {
        this.$message({
          type: 'info',
          message: '请先勾选设备',
        });
        return;
      }
      const deletearr = [];
      for (let i = 0; i < this.choosedp.length; i += 1) {
        deletearr.push(this.choosedp[i].hash.id);
      }
      // console.log(deletearr, '2132');

      this.$confirm('确认删除已选择的设备?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          const req = {
            handle:
              'DD5D108B99FC12B72DC0B97F927BBE3DEAA0F765F26818C2BFF0B9210751F692',
            id: deletearr,
          };
          this.spost(this.ts, '/mainservlet', req).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功',
            });
            this.geteqlist();
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    // 获取设备列表
    geteqlist (p) {
      if (p) {
        this.pagobj.currentPage3 = p;
      }
      if (this.jddate == null) {
        this.jddate = [];
      }
      if (this.jddate2 == null) {
        this.jddate2 = [];
      }
      if (this.buydate == null) {
        this.buydate = [];
      }
      const req = this.searchData;
      req.handle = 'DD5D108B99FC12B72DC0B97F927BBE3D1A8639331D9DCC49';
      req.start = (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize;
      req.limit = this.pagobj.pagesize;
      [req.verifiytime_begin] = [this.jddate[0]];
      [req.verifiytime_end] = [this.jddate[1]];
      [req.nextverifiytime_begin] = [this.jddate2[0]];
      [req.nextverifiytime_end] = [this.jddate2[1]];
      [req.verifiytime_begin] = [this.buydate[0]];
      [req.verifiytime_end] = [this.buydate[1]];
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.eqlist = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
      this.geteqlistsssssss();
    },
    // 获取设备列表
    geteqlistsssssss () {
      if (this.jddate == null) {
        this.jddate = [];
      }
      if (this.jddate2 == null) {
        this.jddate2 = [];
      }
      if (this.buydate == null) {
        this.buydate = [];
      }
      const req = this.searchData;
      req.handle = 'DD5D108B99FC12B72DC0B97F927BBE3D1A8639331D9DCC49';
      req.start = (this.pagobj.currentPage3 - 1) * this.pagobj.pagesize;
      req.limit = 1000000000;
      [req.verifiytime_begin] = [this.jddate[0]];
      [req.verifiytime_end] = [this.jddate[1]];
      [req.nextverifiytime_begin] = [this.jddate2[0]];
      [req.nextverifiytime_end] = [this.jddate2[1]];
      [req.verifiytime_begin] = [this.buydate[0]];
      [req.verifiytime_end] = [this.buydate[1]];
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.neweqlist = ts.d.data.operation1;
        this.pagobj.total = parseInt(ts.d.data.operation2[0].hash.allrs);
      });
    },
    // 点击新增人员
    showneweq () {
      this.isedit = false;
      this.reload('isneweq');
    },
    // 公共组件刷新方法
    reload (partsname) {
      this[partsname] = false;
      this.$nextTick(() => {
        this[partsname] = true;
      });
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]));
    },
    // 导出功能
    daochu () {
      if (this.eqlist.length == 0) {
        this.$message({
          type: 'error',
          message: '导出数据为空,无法导出',
        });
        return false;
      }

      const xlsxParam = { raw: true }; // 转换成excel时，使用原始的格式
      /* generate workbook object from table */
      const wb = XLSX.utils.table_to_book(
        document.querySelector('#tableExcel2ssss'),
        xlsxParam,
      );
      console.log(wb);
      /* get binary string as output */
      const wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array',
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream;charset=utf-8' }),
          'sheetjs.xlsx',
        );
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout);
      }
      return wbout;
    },
  },
};
</script>

